package com.gzh.core.model.vo;

import com.gzh.core.util.exception.BusinessException;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.springframework.http.ResponseEntity;
import org.springframework.lang.Nullable;

/**
 * @Author guanzh
 * @Date 2022/1/3 13:15
 * @Description
 */
@Setter
@Getter
@ToString
@NoArgsConstructor
public class Result<T>  extends BaseResult{
    private T data;//数据

    public Result(String errorCode, String message) {
        this.setCode(errorCode);
        this.setMessage(message);
    }

    public Result(@Nullable T data) {
        this.data = data;
    }


    public static Result<?> defineError(BusinessException e) {
        return new Result<>(e.getErrorCode(),e.getMessage());
    }

    public static <T> ResponseEntity<?> ok(@Nullable T data) {
        return ResponseEntity.ok(new Result<>(data));
    }

    public static <T> ResponseEntity<?> ok() {
        return ResponseEntity.ok(new Result<>());
    }
}
